﻿#include <QDateTime>
#include <QColor>
#include "mark_layer.h"

MarkLayer::MarkLayer(QCustomPlot *plot, QString target_layer, QCPLayerable *parent_layerable)
    : QCPLayerable(plot, target_layer, parent_layerable)
{
}

MarkLayer::~MarkLayer()
{

}

void MarkLayer::CreateMarkTip(QCPGraph* graph, QPointF p, const QStringList& text)
{
	MarkTipWidget* tip_widget = new MarkTipWidget(parentPlot());
	mark_point_list_.append(MarkPoint(tip_widget, graph, p, text));
}

void MarkLayer::SetColor(const QString& bg_color)
{
	_kMarkTipBackgroundColor = QColor(bg_color.toUInt(NULL, 16));
}

int MarkLayer::GetMarkTipCount()
{
	return mark_point_list_.count();
}

void MarkLayer::SetMarkTipVisible(bool visible)
{
	foreach(auto mark_point, mark_point_list_)
	{
		mark_point.GetMarkTipWidget()->setVisible(visible);
	}
}

void MarkLayer::RemoveOneMarkTip(MarkTipWidget* tip_widget)
{
	foreach(auto mark_point, mark_point_list_)
	{
		if (mark_point.MatchMarkTipWidget(tip_widget))
		{
			mark_point.GetMarkTipWidget()->setParent(NULL);
			mark_point.Clear();
			break;
		}
	}
}

void MarkLayer::RemoveOneMarkTip(QCPGraph* graph, QPointF p)
{
	foreach(auto mark_point, mark_point_list_)
	{
		if (mark_point.MatchPointSource(graph, p))
		{
			mark_point.GetMarkTipWidget()->setParent(NULL);
			mark_point.Clear();
			break;
		}
	}
}

void MarkLayer::RemoveAllMarkTip()
{
	foreach(auto mark_point, mark_point_list_)
	{
		mark_point.Clear();
	}
}
void MarkLayer::draw(QCPPainter * painter)
{
	/*
	1.获取可视区域
	2.判断标记点是否在可视区域
	1、获取到曲线名字，
	2、获取到曲线颜色
	3、画一个背景
	4、先画时间，再画图+ 值
	*/

	painter->setFont(QFont("黑体", 8));
	QPen pen;
	pen.setWidth(2);
	pen.setColor(QColor(Qt::red));
	painter->setPen(pen);
	painter->setRenderHint(QPainter::SmoothPixmapTransform);
	QFontMetrics font_metrics = painter->fontMetrics();

	foreach(auto mark_point, mark_point_list_)
	{
		QCPRange xrange = parentPlot()->xAxis->range();//x轴范围
		QCPRange yrange = parentPlot()->yAxis->range();//y轴范围
		QRectF xy_rect(QPointF(xrange.lower, yrange.upper), QPointF(xrange.upper, yrange.lower));//可视区域
		QPair<QCPGraph*, QPointF> pp = mark_point.GetPointSource();//标签源
		MarkTipWidget* mark_tip_widget = mark_point.GetMarkTipWidget();//标签界面
		//判断点是否在可视区域
		if (!xy_rect.contains(pp.second))
		{
			mark_tip_widget->setVisible(false);
			continue;
		}

		QPixmap pixmap(500, 500);
		pixmap.fill(_kMarkTipBackgroundColor);
		QPainter pixmap_painter;
		int row_redge = painter->fontMetrics().height();//设置字体像素高度
		int row_count = 0;             //记录画了几行数据，计算大小
		int max_length = 0;           //记录所画字符串最大长度

		pixmap_painter.begin(&pixmap);
		pixmap_painter.setPen(Qt::white);
		pixmap_painter.setRenderHint(QPainter::SmoothPixmapTransform);
		QStringList line_text = mark_point.GetMarkTipText();

		int pixmap_row_redge = row_redge + 2;
		//属性值 画字
		for (int line = 0; line < line_text.count(); ++line)
		{
			QString text = line_text.at(line);
			pixmap_painter.setPen(pp.first->pen());
			pixmap_painter.drawText(3, pixmap_row_redge  * (row_count+1), text);
			row_count++;//计算文本行数
			//计算字体长度
			int length = font_metrics.width(text);
			max_length = max_length > length ? max_length : length;//获取最长的长度
		}

		//转换为曲线视图中的位置
		double xpixel_pos = parentPlot()->xAxis->coordToPixel(pp.second.x());
		double ypixel_pos = parentPlot()->yAxis->coordToPixel(pp.second.y());
		QPoint pixel_pos(xpixel_pos, ypixel_pos);

		mark_tip_widget->setFixedSize(max_length + 6, row_count * pixmap_row_redge + 6);
		//左下角
		if (parentPlot()->pos().x() + pixel_pos.x() > parentPlot()->pos().x() + mark_tip_widget->width() + 10 &&
			pixel_pos.y() < parentPlot()->pos().y() + parentPlot()->height() - mark_tip_widget->height() - 10)
		{
			mark_tip_widget->move(pixel_pos.x() - mark_tip_widget->width() - 10, pixel_pos.y() + 10);
		}
		//右下角
		else if (parentPlot()->pos().x() + pixel_pos.x() < parentPlot()->pos().x() + parentPlot()->width() - mark_tip_widget->width() - 10 &&
			pixel_pos.y() < parentPlot()->pos().y() + parentPlot()->height() - mark_tip_widget->height() - 10)
		{
			mark_tip_widget->move(pixel_pos.x() + 10, pixel_pos.y() + 10);
		}
		//左上角
		else if (parentPlot()->pos().x() + pixel_pos.x() > parentPlot()->pos().x() + mark_tip_widget->width() + 10 &&
			pixel_pos.y() > parentPlot()->pos().y() + mark_tip_widget->height() + 10)
		{
			mark_tip_widget->move(pixel_pos.x() - 10, pixel_pos.y() - 10);
		}
		//右上角
		else if (parentPlot()->pos().x() + pixel_pos.x() < parentPlot()->pos().x() + parentPlot()->width() - mark_tip_widget->width() - 10 &&
			pixel_pos.y() > parentPlot()->pos().y() - mark_tip_widget->height() + 10)
		{
			mark_tip_widget->move(pixel_pos.x() + 10, pixel_pos.y() - 10);
		}
		else
		{

		}
		pixmap_painter.end();
		mark_tip_widget->SetPixmap(pixmap);
		mark_tip_widget->setVisible(true);
	}
}


MarkPoint::MarkPoint()
	: tip_widget_(NULL)
	, graph_(NULL)
	, p_(QPointF())
{
}

MarkPoint::MarkPoint(MarkTipWidget* tip_widget, QCPGraph* graph, QPointF p, const QStringList& text)
	: tip_widget_(tip_widget)
	, graph_(graph)
	, p_(p)
	, text_(text)
{
}

void MarkPoint::SetParameter(MarkTipWidget* tip_widget, QCPGraph* graph, QPointF p, const QStringList& text)
{
	tip_widget_ = tip_widget;
	graph_ = graph;
	p_ = p;
	text_ = text;
}

bool MarkPoint::MatchPointSource(const QCPGraph* graph, const QPointF p)
{
	if (graph == graph_ && p == p_)
	{
		return true;
	}
	return false;
}

MarkTipWidget* MarkPoint::GetMarkTipWidget()
{
	return tip_widget_;
}

bool MarkPoint::MatchMarkTipWidget(const MarkTipWidget* tip_widget)
{
	if (tip_widget == tip_widget_)
	{
		return true;
	}
	return false;
}

QPair<QCPGraph*, QPointF> MarkPoint::GetPointSource()
{
	return QPair<QCPGraph*, QPointF>(graph_, p_);
}

QStringList MarkPoint::GetMarkTipText()
{
	return text_;
}

void MarkPoint::Clear()
{
	tip_widget_ = NULL;
	graph_ = NULL;
	p_ = QPointF();
	text_.clear();
}
